<template>
  <div v-if="visible" class="confirm-overlay">
    <div class="confirm-dialog">
      <div class="confirm-content">
        <h3>{{ title }}</h3>
        <p>{{ message }}</p>
      </div>
      <div class="confirm-actions">
        <button @click="handleConfirm" class="confirm-button confirm">确认</button>
        <button @click="handleCancel" class="confirm-button cancel">取消</button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';

const props = defineProps({
  title: {
    type: String,
    default: '确认'
  },
  message: {
    type: String,
    required: true
  }
});

const visible = ref(false);
let resolvePromise = null;

const show = () => {
  visible.value = true;
  return new Promise((resolve) => {
    resolvePromise = resolve;
  });
};

const handleConfirm = () => {
  visible.value = false;
  if (resolvePromise) resolvePromise(true);
};

const handleCancel = () => {
  visible.value = false;
  if (resolvePromise) resolvePromise(false);
};

defineExpose({ show });
</script>

<style scoped>
.confirm-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 9999;
}

.confirm-dialog {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: white;
  border-radius: 8px;
  padding: 20px;
  width: 300px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
  animation: fadeIn 0.3s ease-out;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translate(-50%, -40%);
  }
  to {
    opacity: 1;
    transform: translate(-50%, -50%);
  }
}

.confirm-content {
  margin-bottom: 20px;
}

.confirm-content h3 {
  margin-top: 0;
  color: #333;
}

.confirm-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.confirm-button {
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.2s;
}

.confirm-button.confirm {
  background-color: #1890ff;
  color: white;
}

.confirm-button.confirm:hover {
  background-color: #40a9ff;
}

.confirm-button.cancel {
  background-color: #f0f0f0;
  color: #333;
}

.confirm-button.cancel:hover {
  background-color: #d9d9d9;
}
</style>
